﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SmallControls
{
    /// <summary>
    /// Interaction logic for userSearchTextBoxes.xaml
    /// </summary>
    public partial class userSearchTextBoxes : UserControl
    {
        public delegate void ChangedEventHandler(object sender, ChangedSearchTextArgs e);
        public event ChangedEventHandler ChangedSearchedText;
        public List<TextBox> textBoxes = new List<TextBox>();
        public string tableSearched = "Nevracene";
        public userSearchTextBoxes()
        {
            InitializeComponent();
            initBoxes();
            cboxTable.SelectionChanged += cboxTable_SelectionChanged;
            cboxTable.DropDownOpened += cboxTable_DropDownOpened;
            cboxTable.DropDownClosed += cboxTable_DropDownClosed;
            cboxTable.Loaded += cboxTable_Loaded;
        }

        void cboxTable_DropDownClosed(object sender, EventArgs e)
        {
            if (IsLoaded && !cboxRefreshing)
            {
                tableSearched = ((ListBoxItem)cboxTable.SelectedItem).Content.ToString();
                if (ChangedSearchedText != null)
                    ChangedSearchedText(this, new ChangedSearchTextArgs(GetDictionarys(), tableSearched));
            }
        }

        void cboxTable_DropDownOpened(object sender, EventArgs e)
        {
            ReloadCombobox();
        }

        void cboxTable_Loaded(object sender, RoutedEventArgs e)
        {
            ReloadCombobox();
        }

        public void ReloadCombobox()
        {
            cboxRefreshing = true;
            List<string> tables = DataLib.Settings.databases.dbPujcky.QueryGetListString("SELECT table_name from information_schema.tables");
            cboxTable.Items.Clear();
            foreach (string table in tables)
            {
                ListBoxItem item = new ListBoxItem();
                item.Content = table;
                cboxTable.Items.Add(item);
                cboxTable.SelectedIndex = 0;
            }
            cboxRefreshing = false;
        }

        void cboxTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
        }
        bool cboxRefreshing = false;

        private void initBoxes()
        {
            textBoxes.Add(jmeno);
            textBoxes.Add(cena);
            textBoxes.Add(zboziID);
            textBoxes.Add(zboziNazev);
            textBoxes.Add(JIScislo);
            textBoxes.Add(prijmeni);
            textBoxes.Add(poznamka);
        }

        public Dictionary<string,string> GetDictionarys()
        {
            Dictionary<string,string> resultt = new Dictionary<string,string>();
            foreach(TextBox txt in textBoxes)
            {
                if (txt.Text != " " && txt.Text != "")
                    resultt.Add(txt.Name, txt.Text);
            }
           /* if(dpOd.SelectedDate!= null)
            resultt.Add("od", dpOd.SelectedDate.Value.ToString("yyyy-MM-dd hh:mm:ss"));
            if (dpDo.SelectedDate != null)
            resultt.Add("do", dpDo.SelectedDate.Value.ToString("yyyy-MM-dd hh:mm:ss"));*/
            return resultt;
        }

        private void TextChanged_1(object sender, TextChangedEventArgs e)
        {
            if (ChangedSearchedText != null)
                ChangedSearchedText(this, new ChangedSearchTextArgs(GetDictionarys(), tableSearched));
        }

        private void datum_TextChanged_1(object sender, TextChangedEventArgs e)
        {

        }

        private void btnSmazat_Click(object sender, RoutedEventArgs e)
        {
            foreach (TextBox txt in textBoxes)
                txt.Clear();
        //  dpOd.SelectedDate = null;
         // dpDo.SelectedDate = null;
           // dpOd.ClearValue();
        }

        private void DatePicker_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (ChangedSearchedText != null)
                ChangedSearchedText(this, new ChangedSearchTextArgs(GetDictionarys(), tableSearched));
        }
    }

    public class ChangedSearchTextArgs
    {
        public ChangedSearchTextArgs(Dictionary<string, string> searchedText, string searchedTable)
        {
            this.table = searchedTable;
            this.searchedText = searchedText;
        }
        public string table = "Nevracene";
        public Dictionary<string, string> searchedText = new Dictionary<string, string>();
        public void GetStringForSQLSearch()
        {
            
        }
    }
}
